<?php
	$table="thanhphan";
	extract($_GET);
	include("../../include/dbconnect.php");
	$page=0;
	if(isset($_POST["page"]))
		if(is_numeric($_POST["page"])) $page=(int)$_POST["page"];
	if($page==0) $page=1;
	
	$rp=0;
	if(isset($_POST["rp"]))
		if(is_numeric($_POST["rp"])) $rp=(int)$_POST["rp"];
	if($rp==0) $rp=15;
	
	
	$sidx="";
	if(isset($_POST["sortname"]))
		$sidx=$_POST["sortname"];
	if($sidx=="") $sidx="1";

	if($sidx=="tenmenumuc") $sidx="(select f3.ten from menumuc as f3 where f3.id=f.idmenumuc)";
	else if($sidx!="1") $sidx="f.".$sidx;
	
	$sord="";
	if(isset($_POST["sortorder"])) $sord=$_POST["sortorder"];
	
	$query="";
	if(isset($_POST["query"]))
		if($_POST["query"]!="") $query=$_POST["query"];
		
	$qtype="";
	if(isset($_POST["qtype"]))
		if($_POST["qtype"]!="") $qtype=$_POST["qtype"];

	if($qtype=="tenmenumuc") $qtype="(select f2.ten from menumuc as f2 where f2.id=f.idmenumuc)";
	else $qtype="f.".$qtype;
	
	$where="WHERE f.idmenumuc=$mnid";
	
	if($query!="") $where=" WHERE (f.idmenumuc=$mnid) and ($qtype LIKE '%$query%') ";
	
	$SQL="SELECT f.*,";
	$SQL=$SQL."(select f1.ten from menumuc as f1 where f1.id=f.idmenumuc) as tenmenumuc ";
	$SQL=$SQL."FROM $table as f $where";
	$result = mysql_query($SQL,$conn) or die($SQL);
	$count = mysql_num_rows($result);
	mysql_free_result($result);
 
	$start = (($page-1) * $rp);
 
	$SQL=$SQL." ORDER BY $sidx $sord LIMIT $start,$rp";
	$result = mysql_query($SQL,$conn) or die($SQL);
 

    header("Content-type: text/xml"); 
	echo "<?xml version='1.0' encoding='utf-8' ?>"; 
	echo "<rows>"; 
	echo "<page>".$page."</page>"; 
	echo "<total>".$count."</total>";
 
	while($row = mysql_fetch_array($result)) { 
		echo "<row id='". $row["id"]."'>";
		echo "<cell>".$row["id"]."</cell>";
		echo "<cell><![CDATA[".$row["tenmenumuc"]."]]></cell>";
		echo "<cell><![CDATA[".date("d-m-Y H:i",strtotime($row["ngay"]))."]]></cell>";
		echo "<cell><![CDATA[".$row["mota"]."]]></cell>";
		echo "<cell><![CDATA[".(($row["chophep"]==1)?"Cho phép":"Cấm")."]]></cell>";
		echo "<cell><![CDATA[".$row["ngaytao"]."]]></cell>";
		echo "<cell><![CDATA[".$row["un"]."]]></cell>";
		echo "</row>"; 
	}
echo "</rows>"; 
mysql_free_result($result);
include("../../include/dbclose.php");
?>
